package com.barbeque.mall.controller.wx;

import com.barbeque.mall.bean.BasePageInfo;
import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.advertise.VO.MyBaseRespVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.user.MarketUser;
import com.barbeque.mall.bean.wx.coupon.BO.WxMarketCouponMyListBo;
import com.barbeque.mall.bean.wx.coupon.WxMarketCoupon;
import com.barbeque.mall.bean.wx.coupon.WxMarketCouponUser;
import com.barbeque.mall.service.wx.WxCouponService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author bai
 * @since 2022/07/19 17:44
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {
    @Autowired
    WxCouponService wxCouponService;


    // localhost:8083/wx/coupon/list?page=1&limit=10
    @RequestMapping("list")
    public BaseRespVo list(BasePageInfo basePageInfo){

        CommonData<WxMarketCoupon> couponList = wxCouponService.queryAllCouponList(basePageInfo);

        return BaseRespVo.ok(couponList);
    }

    // localhost:8083/wx/coupon/mylist?status=0&page=1&limit=10
    @RequestMapping("mylist")
    public BaseRespVo mylist(WxMarketCouponMyListBo wxMarketCouponMyListBo){

        CommonData<WxMarketCoupon> myCouponList = wxCouponService.queryMyCouponList(wxMarketCouponMyListBo);



        return BaseRespVo.ok(myCouponList);
    }

    // localhost:8083/wx/coupon/selectlist?cartId=329&grouponRulesId=0
    @RequestMapping("selectlist")
    // 根据cartId查到userId
    // 根据userId查到couponId
    // 根据couponId查到对应的优惠券信息
    // 返回值data为对应的优惠券信息
    // 可以写成多表查询
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId) throws ParseException {

        CommonData<WxMarketCoupon> couponList = wxCouponService.queryAvailableCoupon(cartId,grouponRulesId);

        return BaseRespVo.ok(couponList);
    }

    // localhost:8083/wx/coupon/receive
    //  可以使用多表查询
    @RequestMapping("receive")
    public MyBaseRespVo receive(@RequestBody Map map,HttpServletResponse resp) throws Exception {

        int couponId = (int) map.get("couponId");
        // T通过shiro获取userId
        MarketUser primaryPrincipal = (MarketUser) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        Integer userId = primaryPrincipal.getId();

        // 根据userId查询couponId
        List<WxMarketCouponUser> wxMarketCouponUsers = wxCouponService.selectCouponUser(userId);

        for (WxMarketCouponUser wxMarketCouponUser : wxMarketCouponUsers) {
            if(wxMarketCouponUser.getCouponId() == couponId){
                return MyBaseRespVo.fail();
            }
        }

        // 执行到这里说明当前用户未持有此种优惠券
        // 首先查询coupon表中此种优惠券的库存，如果为-1表示没有库存，无法获得
        WxMarketCoupon wxMarketCoupon = wxCouponService.queryCouponTotalByCouponId(couponId);



        if(wxMarketCoupon.getTotal() != -1){
            // 将优惠券信息添加到user_coupon表
            wxCouponService.insertCouponUserByCouponId(userId,couponId);
            // 减优惠券库存
            wxCouponService.updateCouponTotalByCouponId(wxMarketCoupon);
        }else {
            return MyBaseRespVo.noToal();
        }

        return MyBaseRespVo.ok();

    }

    // localhost:8083/wx/coupon/exchange
    @RequestMapping("exchange")
    public MyBaseRespVo exchange(@RequestBody Map map,HttpServletResponse resp) throws Exception {
        String code = (String) map.get("code");

        WxMarketCoupon wxMarketCoupon = wxCouponService.queryCouponByCode(code);
        if(wxMarketCoupon == null){
            return MyBaseRespVo.wrongCode();
        }else {
            map.put("couponId",wxMarketCoupon.getId());
            MyBaseRespVo receive = receive(map,resp);
            if(receive.getErrno() == 740 && receive.getErrmsg().equals("优惠券已经领取过")){
                return MyBaseRespVo.rightCode();
            }
        }
        return MyBaseRespVo.ok();
    }

}
